<template>
  <div class="count-item">
    <div class="top">
      <img :class="iconClass" :src="iconSrc" />
      <div>
        <div class="text">{{ label }}</div>
        <div class="count" :style="{ color: countColor }">
          {{ !currentValue ? 0 : +currentValue?.toFixed(2)
          }}<span v-if="unit" class="unit">{{ unit }}</span>
        </div>
      </div>
    </div>

    <div class="rate-info" v-if="label !== '待批改任务数'">
      <div class="text">较上周</div>
      <div class="rate" :style="{ color: rateColor }">
        <el-icon color="#FF9686" v-if="isPositiveChange">
          <IEpCaretTop />
        </el-icon>
        <el-icon color="#4EC680" v-if="isNegativeChange">
          <IEpCaretBottom />
        </el-icon>
        <span class="flatten" v-if="isNoChange"></span>
        {{ improvement }}
      </div>
    </div>
    <div class="rate-info" v-if="label === '待批改任务数'"></div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  iconClass: String,
  iconSrc: String,
  label: String,
  countColor: String,
  currentValue: [Number, String],
  previousValue: [Number, String],
  unit: String
})

const calculateImprovement = (lastWeek, currentWeek) => {
  if (lastWeek === 0) return '--'
  const value = (((currentWeek - lastWeek) / Math.abs(lastWeek)) * 100).toFixed(
    2
  )
  if (+value === 0) return '持平'
  if (value === 'NaN') return '--'
  return value + '%'
}

const calculateColor = (current, previous) => {
  if (!previous) {
    return '#EAA433'
  }
  if (current > previous) {
    return '#FF9686'
  } else if (current < previous) {
    return '#4EC680'
  } else {
    return '#EAA433'
  }
}

const improvement = computed(() =>
  calculateImprovement(props.previousValue, props.currentValue)
)
const rateColor = computed(() =>
  calculateColor(props.currentValue, props.previousValue)
)
const isPositiveChange = computed(() => {
  if (!props.previousValue) {
    return false
  }
  return props.currentValue > props.previousValue
})
const isNegativeChange = computed(() => {
  if (!props.previousValue) {
    return false
  }
  return props.currentValue < props.previousValue
})
const isNoChange = computed(() => props.currentValue === props.previousValue)
</script>

<style lang="less" scoped>
.count-item {
  box-sizing: border-box;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  width: 241px;
  height: 132px;
  padding: 15px;
  border-radius: 5px;
  background-color: #fff;

  .top {
    flex: 1;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px dashed #f5f5f5;
    .icon {
      width: 70px;
      height: 70px;
      margin-bottom: 10px;
    }

    .rate-info {
      height: 41px;
      gap: 6.8px;

      .text {
        color: rgb(51 51 51 / 0.6);
      }

      .flatten {
        display: inline-block;
        width: 10px;
        height: 10px;
        background-color: #f1b44a;
        border-radius: 50%;
        margin-right: 5px;
      }
    }
  }

  .text {
    font-size: 16px;
    letter-spacing: 0.7px;
    margin-bottom: 1px;
    font-weight: 500;
  }

  .count {
    font-size: 30px;
    font-weight: bold;

    .unit {
      font-size: 14px;
    }
  }

  .rate-info {
    display: flex;
    align-items: center;
    gap: 6.8px;
    margin-top: 5px;
    height: 41px;

    .text {
      color: rgb(51 51 51 / 0.6);
    }
  }
}
</style>
